package com.wch.SpringBootTestDemo.defineTest;

import java.io.IOException;

import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.boot.env.PropertySourcesLoader;


public class YamlPropertySourceFactory implements PropertySourceFactory {

	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		@SuppressWarnings("rawtypes")
		PropertySource test = name != null ? new PropertySourcesLoader().load(resource.getResource(), name, null) : new PropertySourcesLoader().load(  
                resource.getResource(), getNameForResource(resource.getResource()), null); 
		
		return test;
	}
	
	private static String getNameForResource(Resource resource) {  
        String name = resource.getDescription();  
        if (!org.springframework.util.StringUtils.hasText(name)) {
            name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);  
        }  
        return name;  
    }


}
